//
//  GeneralViewController.m
//  IBaby
//
//  Created by ZengYuan on 14/11/27.
//  Copyright (c) 2014年 com.zhongwei. All rights reserved.
//

#import "GeneralViewController.h"
#import "SwitchSetCell.h"
#import "Config.h"

@interface GeneralViewController ()

@end

@implementation GeneralViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.view.backgroundColor=RGB(232, 233, 232);
    
    // Do any additional setup after loading the view.
    
    tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    tableView.delegate=self;
    tableView.dataSource=self;
    tableView.backgroundColor=[UIColor clearColor];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:tableView];
    
    tableView.contentInset = UIEdgeInsetsMake(20, 0.0f, 0.0f, 0.0f);

    // Do any additional setup after loading the view.
    dataArray=[NSArray arrayWithObjects:[NSArray arrayWithObjects: @"非WIFI网络播放提醒", nil],nil];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.titleLabel.text=@"设置";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [dataArray[section] count];
}




- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellActivity = @"CellActivity";
    SwitchSetCell *cell=[_tableView dequeueReusableCellWithIdentifier:CellActivity];
    if (!cell) {
        cell=[[SwitchSetCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellActivity];
    }
    cell.backgroundColor=[UIColor whiteColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSArray *array=dataArray[indexPath.section];
    cell.textLabel.text=[array objectAtIndex:indexPath.row];
    cell.rowType = [self rowIndex:indexPath];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

//    if (indexPath.row==0) {
//        cell.toggle.on = [self allowLandscape];
//        switch_lands=cell.toggle;
//    }else{
        cell.toggle.on = [self allowNoWIFIAlert];
        switch_nowifi=cell.toggle;

//    }
    
    return cell;
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
    NSUserDefaults*defaults = [NSUserDefaults standardUserDefaults];
    if (switch_lands.on) {
        [defaults setObject:@"KSWITCH_LANDSCAPE" forKey:KSWITCH_LANDSCAPE];
    }else{
        [defaults removeObjectForKey:KSWITCH_LANDSCAPE];
    }
    
    if (switch_nowifi.on) {
        [defaults setObject:@"KSWITCH_NOWIFIALERT" forKey:KSWITCH_NOWIFIALERT];
    }else{
        [defaults removeObjectForKey:KSWITCH_NOWIFIALERT];
    }
 }

-(int)rowIndex:(NSIndexPath *)indexPath{
    int rowsCount = [dataArray[indexPath.section] count];
    
    if (rowsCount>1) {
        if (indexPath.row==0) {
            return 1;
        }else if(indexPath.row == rowsCount-1){
            return 3;
        }
        return 2;
        
    }else{
        return 0;
    }
}

-(BOOL)allowLandscape{
    
    return [[NSUserDefaults standardUserDefaults] objectForKey:KSWITCH_LANDSCAPE]?YES:NO;
}

-(BOOL)allowNoWIFIAlert{
    
    return [[NSUserDefaults standardUserDefaults] objectForKey:KSWITCH_NOWIFIALERT]?YES:NO;
}

@end
